package com.dushibao.admin.mv;

import com.dushibao.admin.handler.SpringContextHolder;
import com.dushibao.admin.model.Admin;
import com.dushibao.admin.model.SysConfig;
import com.dushibao.admin.service.IMenuService;
import com.dushibao.admin.service.ISysConfigService;
import com.dushibao.admin.vo.request.MenuModelPageRequest;
import com.dushibao.utils.PageRequest;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DushibaoModelAndView extends ModelAndView {

    public DushibaoModelAndView(String viewName){
        this(viewName,new PageRequest());
    }

    public DushibaoModelAndView(String viewName, PageRequest pageRequest){
        super.setViewName(viewName);
        //在这里初始化一些参数

        ISysConfigService sysConfigService = SpringContextHolder.getBean(ISysConfigService.class);

        Map<String, Object> sysConfig = new HashMap<>();

        List<SysConfig> sysConfigs = sysConfigService.list();

        sysConfigs.forEach(e->{
            sysConfig.put(e.getSysKey(),e.getSysValue());
        });

        super.addObject("sysConfig",sysConfig);

        IMenuService menuService = SpringContextHolder.getBean(IMenuService.class);

        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

        HttpSession session = requestAttributes.getRequest().getSession();
        Admin admin = (Admin) session.getAttribute("admin");
        super.addObject("menus",menuService.findAdminMenuList(admin.getId()));



        if(pageRequest.getOp()!=null && pageRequest.getOpc()!=null){
            session.setAttribute("op",pageRequest.getOp());
            session.setAttribute("opc",pageRequest.getOpc());
        }

        super.addObject("modelPageRequest",pageRequest);

    }

}
